﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.IDAL;
using KPIS.GERP.GFMS.DALFactory;

// test only
using KPIS.GERP.GFMS.DAL;

namespace KPIS.GERP.GFMS.BLL
{
    public class B_BGM_ProjectOperation : IBLL_BGM_ProjectOperation
    {
        Common common = new Common();
        private static readonly IDAL_BGM_ProjectOperation dal = DataAccess.Create_BGM_ProjectOperation();

        public B_BGM_ProjectOperation()
        {

        }

        public IList<M_BGM_ProjectOperation> GetFull(string bgm_source_seq, string bgm_year
            , string job_site_seq, string bgm_proj_seq
            , string use_date, bool IsUse, bool ApproveOnly)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> njob_site_seq = common.ConvertNullInt(job_site_seq);
            Nullable<int> nbgm_proj_seq = common.ConvertNullInt(bgm_proj_seq);
            Nullable<DateTime> nuse_date = common.ConvertNullDateTime(use_date);

            return dal.GetFull(nbgm_source_seq, nbgm_year, njob_site_seq
                , nbgm_proj_seq, nuse_date, IsUse, ApproveOnly);
        }

        public IList<M_BGM_ProjectOperation> GetList(string bgm_source_seq, string bgm_year
            , string job_site_seq, string bgm_proj_seq
            , string use_date, bool IsUse, bool ApproveOnly)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> njob_site_seq = common.ConvertNullInt(job_site_seq);
            Nullable<int> nbgm_proj_seq = common.ConvertNullInt(bgm_proj_seq);
            Nullable<DateTime> nuse_date = common.ConvertNullDateTime(use_date);

            return dal.GetList(nbgm_source_seq, nbgm_year, njob_site_seq
                , nbgm_proj_seq, nuse_date, IsUse, ApproveOnly);
        }

        public IList<M_BGM_ProjectOperation> GetProjectListByEmpSeq(string bgm_year, string bgm_source_seq, string emp_seq)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nemp_seq = common.ConvertNullInt(emp_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);

            return dal.GetProjectListByEmpSeq(nbgm_year, nbgm_source_seq, nemp_seq);
        }

        public IList<M_BGM_ProjectOperation> GetListByBgmOptionSeq(string bgm_year, string bgm_option_seq)
        {
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> nbgm_option_seq = common.ConvertNullInt(bgm_option_seq);

            return dal.GetListByBgmOptionSeq(nbgm_year, nbgm_option_seq);
        }

        public M_BGM_ProjectOperation GetInfo(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.GetInfo(nSeq);
            }
            else
            {
                return null;
            }
        }

        public bool HaveAmountExist(string bgm_proj_oper_seq)
        {
            int nSeq;

            if (bgm_proj_oper_seq == null)
                return false;

            if (common.IsNumeric(bgm_proj_oper_seq))
            {
                nSeq = int.Parse(bgm_proj_oper_seq);
                return dal.HaveAmountExist(nSeq);
            }
            else
            {
                return false;
            }
        }

        //public string SelectRecentInsertedRoleSeq()
        //{
        //    return dal.SelectRecentInsertedRoleSeq();
        //}

        public string Insert(M_BGM_ProjectOperation info)
        {
            try
            {
                return dal.Insert(info);
            }
            catch
            {
                throw;
            }
        }

        public void Update(M_BGM_ProjectOperation info)
        {
            try
            {
                dal.Update(info);
            }
            catch
            {
                throw;
            }
        }

        public void Delete(M_BGM_ProjectOperation info)
        {
            try
            {
                dal.Delete(info);
            }
            catch
            {
                throw;
            }
        }

        public bool ChangeStatus(M_BGM_ProjectOperation info)
        {
            try
            {
                if (info.record_status != "A")
                {
                    dal.ChangeStatus(info);
                    return true;
                }
                else if (!dal.HaveAmountExist(info.bgm_proj_oper_seq))
                {
                    return false;
                }
                else
                {
                    dal.ChangeStatus(info);
                    return true;
                }
            }
            catch
            {
                throw;
            }
        }

        public void CloseOperation(M_BGM_ProjectOperation info)
        {
            try
            {
                dal.CloseOperation(info);
            }
            catch
            {
                throw;
            }
        }

        public DataTable Report_ProjectOperation(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.Report_ProjectOperation(nSeq);
            }
            else
            {
                return null;
            }
        }

        public DataTable SubReport_ProjectOperation(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.SubReport_ProjectOperation(nSeq);
            }
            else
            {
                return null;
            }
        }

        public DataTable Report_ProjectOperationEdit(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.Report_ProjectOperationEdit(nSeq);
            }
            else
            {
                return null;
            }
        }

        public DataTable SubReport_ProjectOperationEdit(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.SubReport_ProjectOperationEdit(nSeq);
            }
            else
            {
                return null;
            }
        }

        public string GetOptionProduct(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.GetOptionProduct(nSeq);
            }
            else
            {
                return null;
            }
        }

        public string ProjectOprationCode(string strBgmProjOperSeq)
        {
           return dal.ProjectOprationCode(strBgmProjOperSeq);
        }
    }
}
